package com.cloudcc.mobile.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.util.SystemUtils;
import com.cloudcc.mobile.view.aduit.AduitNearByMapActivity;
import com.cloudcc.mobile.view.aduit.adapter.AduitNearByPopupAdapter;
import com.cloudcc.mobile.view.nearby.NearByMapActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : 92152
 *     e-mail : xxx@xx
 *     time   : 2020/04/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class AduitNearByPopupwindow extends PopupWindow {

    ListView listview;


    public String longs;

    private View conentView;

    private AduitNearByMapActivity mcontext;

    private List<Integer> objects;
    private int mark;//1 距离   2对象
    private AduitNearByPopupAdapter adapter;

    public AduitNearByPopupwindow(final Activity context , int mark) {
        mcontext = (AduitNearByMapActivity) context;
        this.mark = mark;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.near_by_popupwindow, null);
        setDataToView();

        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(PtrFrameLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(PtrFrameLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview3);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);


    }

    private void setLister() {

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mcontext.onItemPopupClick(objects.get(position),mark);
            }
        });
    }

    public void setDataToView(){
        listview = (ListView) conentView.findViewById(R.id.near_by_popup_listview);
        objects = new ArrayList<>();
        if(mark == 1){//千米弹窗
            objects.add(R.string.near_by_1km);
            objects.add(R.string.near_by_3km);
            objects.add(R.string.near_by_5km);
            objects.add(R.string.near_by_maxkm);
        }else{//对象弹窗
            objects.add(R.string.near_by_md);
            objects.add(R.string.near_by_jxs);
        }
        setLister();
        adapter = new AduitNearByPopupAdapter(mcontext);
        listview.setAdapter(adapter);
        adapter.addData(objects);

    }

    public void showPopupWindow(View parent, int x, View topView, int startbarHeight) {
        if (!this.isShowing()) {
            if(conentView.getWidth() == 0){
                x = (int) (x - SystemUtils.dpToPx(62,mcontext));
            }else{
                x =  x - (conentView.getWidth()/2);
            }
            this.showAtLocation(parent, Gravity.NO_GRAVITY, x, startbarHeight + topView.getHeight() + 1);
        } else {
            this.dismiss();
        }
    }



    public interface onItemPopupClick {
        void onItemPopupClick(int obj, int mark);
    }

    @Override
    public void dismiss() {
        mcontext.changeAnima();
        super.dismiss();
    }
}
